package com.solo.eggsolo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.solo.eggsolo.common.bean.JsonResult;
import com.solo.eggsolo.common.bean.Pager;
import com.solo.eggsolo.entity.Recommend;
import com.solo.eggsolo.service.RecommendService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 
 * @author Yu
 *
 */
@RestController
@RequestMapping("/recommend")
@Api(tags = "推荐管理")
public class RecommendController {
	
	@Autowired
	private RecommendService recommendService;
	
	@ApiOperation("获取推荐表信息")
	@RequestMapping(value="/list", method = {RequestMethod.GET})
	public Pager<Recommend> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer size) {
		Pager<Recommend> pager = new Pager<>(page, size);

		List<Recommend> list = recommendService.list(pager);
		int total = recommendService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}
	
	@ApiOperation("根据推荐表id查询信息")
	@RequestMapping(value = "/findById", method = {RequestMethod.GET})
	public JsonResult findById(Integer id) {
		
		JsonResult result = new JsonResult(500,Boolean.FALSE, "查询失败");
		Recommend recommend = recommendService.findById(id);
		
		if (recommend != null) {
			result = new JsonResult(200,Boolean.TRUE, "查询成功",recommend);
		}
	
		return result;
	}
	
	@ApiOperation("增加推荐信息")
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public JsonResult add( Recommend recommend) {
		JsonResult result = new JsonResult(500,Boolean.FALSE, "新增失败");

		//取服务器的系统时间

		boolean ret = recommendService.add(recommend);
		if (ret) {
			result = new JsonResult(200,Boolean.TRUE, "新增成功");
		}
		return result;
	}

	@ApiOperation("根据推荐表id编辑信息")
	@RequestMapping(value = "/edit", method = { RequestMethod.POST })
	public JsonResult edit(Recommend recommend) {
		JsonResult result = new JsonResult(500,Boolean.FALSE, "修改失败");

		
		// 先上传后更新（保存）
		boolean ret = recommendService.edit(recommend);
		if (ret) {
			result = new JsonResult(200,Boolean.TRUE, "修改成功");
		}
		return result;
	}
	
	@ApiOperation("根据推荐表id删除信息")
	@RequestMapping(value = "delete", method = { RequestMethod.POST })
	public JsonResult delete(Recommend recommend) {
		JsonResult result = new JsonResult(500,Boolean.FALSE, "删除失败");

		List<Recommend> list = new ArrayList<Recommend>();
		list.add(recommend);

		boolean ret = recommendService.deleteByIds(list);
		if (ret) {
			result = new JsonResult(200,Boolean.TRUE, "删除成功");
		}
		return result;
	}

}
